package com.hzw.saas.web.admin.user.controller;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Objects;

import com.hzw.saas.api.storage.IStorageService;
import com.hzw.saas.api.storage.bo.StorageCapacityBO;
import com.hzw.saas.api.user.ICustomerService;
import com.hzw.saas.api.user.IUserRightsService;
import com.hzw.saas.api.user.bo.CustomerBO;
import com.hzw.saas.api.user.bo.UserRightsBO;
import com.hzw.saas.api.workspace.IWorkInstanceService;
import com.hzw.saas.api.workspace.IWorkRecordsService;
import com.hzw.saas.api.workspace.bo.AWSSurplusDTO;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.config.util.AssertUtil;
import cn.hutool.core.date.DateUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;

/**
 * @author zzl
 * @since 05/27/2021
 */
@Controller
@RequestMapping
@RequiredArgsConstructor
@Api(tags = "客户数据统计")
@Validated
public class CustomerStatisticsController {

    private final IStorageService storageService;
    private final IWorkRecordsService workRecordsService;
    private final IUserRightsService userRightsService;
    private final IWorkInstanceService workInstanceService;
    private final ICustomerService customerService;

    @GetMapping("/customer/{customerNo}/resource/files/size")
    @SysLog(operation = "统计用户空间容量使用")
    @ApiOperation(value = "统计用户空间容量使用", notes = "统计用户所有资源容量使用信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "customerNo", value = "客户编号", dataType = "String", required = true)
    })
    @ApiOperationSort(1)
    public ResponseEntity<StorageCapacityBO> getStorageCapacity(@PathVariable String customerNo) {
        CustomerBO customer = customerService.getCustomerByNo(customerNo);
        AssertUtil.assertThrow("客户不存在", Objects.isNull(customer));
        StorageCapacityBO storageCapacity = storageService.getStorageCapacity(customer.getUserId(),false);
        return ResponseEntity.ok(storageCapacity);
    }

    @GetMapping("/customer/{customerNo}/aws/info")
    @SysLog(operation = "云控制台机时统计")
    @ApiOperation(value = "云控制台机时统计")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "customerNo", value = "客户编号", dataType = "String", required = true)
    })
    @ApiOperationSort(2)
    public ResponseEntity<AWSSurplusDTO> findAllAWSHistoryByInstId(@PathVariable String customerNo) {
        CustomerBO customer = customerService.getCustomerByNo(customerNo);
        AssertUtil.assertThrow("客户不存在", Objects.isNull(customer));
        UserRightsBO userRightsByUser = userRightsService.getUserRightsByUser(customer.getUserId());
        if (userRightsByUser == null) {
            AWSSurplusDTO awsSurplusDTO1 = new AWSSurplusDTO();
            awsSurplusDTO1.setAwsSurplusBalance(BigDecimal.ZERO);
            awsSurplusDTO1.setAwsTotalBalance(BigDecimal.ZERO);
            awsSurplusDTO1.setDockerRecycleDuration("0");
            awsSurplusDTO1.setEffectiveDate("");
            awsSurplusDTO1.setDockerNumber(0);
            return ResponseEntity.ok(awsSurplusDTO1);
        }
        AWSSurplusDTO awsSurplusDTO = new AWSSurplusDTO();
        awsSurplusDTO.setEffectiveDate(DateUtil.format(userRightsByUser.getAwsCoreTimeExpireTime(), "yyyy-MM-dd"));
        awsSurplusDTO.setAwsTotalBalance(new BigDecimal(userRightsByUser.getAwsCoreTime()));
        Map<String, BigDecimal> allAWSHistoryByInstId = workRecordsService.findAllAWSHistoryByUserId(customer.getUserId());
        BigDecimal bigDecimal = allAWSHistoryByInstId.get(customer.getUserId());
        BigDecimal divide = bigDecimal.divide(new BigDecimal("3600"), 4, 2);
        BigDecimal subtract = new BigDecimal(userRightsByUser.getAwsCoreTime()).subtract(divide);
        awsSurplusDTO.setAwsSurplusBalance(subtract);
        awsSurplusDTO.setDockerNumber(userRightsByUser.getDockerNumber());
        String dockerRecycleDuration = userRightsByUser.getDockerRecycleDuration();
        awsSurplusDTO.setDockerRecycleDuration(dockerRecycleDuration);
        return ResponseEntity.ok(awsSurplusDTO);
    }

}
